package com.spark.colliery.module.agency

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.spark.colliery.R
import com.spark.colliery.module.agency.fragment.AgencySearchDialogFragment
import com.spark.colliery.module.agency.fragment.DiaFilterFragment
import com.spark.colliery.module.agency.fragment.TaskTrackFragment
import com.spark.colliery.module.agency.fragment.TodoFragment
import com.spark.common.BaseFragment
import com.spark.common.ext.setIndicatorPadding
import com.spark.common.helper.MutablePagerAdapter
import kotlinx.android.synthetic.main.frag_agency.*
import me.yokeyword.fragmentation.SupportFragment

/**
 * 待办
 */

class AgencyHomeFragment : BaseFragment() {
    private lateinit var agencyAdapter: MutablePagerAdapter

    companion object {
        fun newInstance(): AgencyHomeFragment {
            val fragment = AgencyHomeFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_agency
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 分类
        ll_category.setOnClickListener { DiaFilterFragment.newInstance().show(childFragmentManager, "DiaFilterFragment") }
        // 搜索
        ll_search.setOnClickListener { AgencySearchDialogFragment.newInstance().show(childFragmentManager, "agencySearchDialogFragment") }

        var tabTitles = mutableListOf("待办任务", "任务跟踪")
        var frags = mutableListOf<Fragment>(
                TodoFragment.newInstance(),
                TaskTrackFragment.newInstance()
        )
        tabTitles.forEach {
            tab.addTab(tab.newTab().setText(it))
        }
        agencyAdapter = object : MutablePagerAdapter(childFragmentManager) {
            override fun getCount(): Int {
                return tabTitles.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return tabTitles[position]
            }
        }
        agencyAdapter.adds(frags)
        pager.adapter = agencyAdapter
        tab.apply {
            setupWithViewPager(pager)
            post {
                setIndicatorPadding(50F,50F)
            }
        }
    }

    fun starz(target: SupportFragment) {
        start(target)
    }
}

